﻿# -*-coding: UTF-8 -*-
import logging
import sys
from pathlib import Path

from PyQt5.QtCore import QTimer

from CloverLauncher.LaunchActionType import LaunchActionType
from CloverConst.CloverEnvironmentType import CloverEnvironmentType
from CloverLauncher.Launcher import register_launch_action
from Globals import CloverEnvironment


class LaunchAction:

	def __init__(self, action_type):
		self.action_type = action_type
		self.finished = False
		self.logger = logging.getLogger(self.__class__.__name__)

	def __repr__(self):
		return 'LaunchAction<%s>' % self.action_type

	def get_description(self):
		"""根据细节返回不同描述"""
		return ""

	def get_progress(self):
		"""基于当前action内的总进度0到100"""
		return 100

	def start(self):
		self.logger.info(f"<{self.__class__.__name__}> start {self.action_type}")
		self.on_start()

	def on_start(self):
		pass

	def is_finished(self):
		return self.finished

	def stop(self):
		self.on_stop()
		self.logger.info(f"<{self.__class__.__name__}> end {self.action_type}")

	def on_stop(self):
		pass

	def mark_finished(self):
		self.finished = True


@register_launch_action(LaunchActionType.ENVIRONMENT_SETUP)
class EnvironmentSetupAction(LaunchAction):

	def __init__(self, action_type):
		super().__init__(action_type)
		self.timer = QTimer()
		self.timer.timeout.connect(self.simulate)
		self.time_count = 0
		self.total_count = 1000 * 20
		self.progress = 0

	def get_description(self):
		return "初始化阿萨法大搜该搜狗i"

	def simulate(self):
		self.time_count += 1
		self.progress = (self.time_count / self.total_count) * 100
		if self.time_count >= self.total_count:
			self.mark_finished()

	def get_progress(self):
		return self.progress

	def on_start(self):
		self.timer.start(1)

	def on_stop(self):
		self.timer.stop()
		self.timer = None
